package com.czy.util;


import com.czy.util.json.JsonUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chenzy
 * @since 2020-03-09
 */
public class MyMap<Key, Value> extends LinkedHashMap<Key, Value> implements Cloneable {
    public MyMap() {
        super();
    }

    @Override
    public MyMap<Key, Value> clone() {
        //noinspection unchecked
        return (MyMap<Key, Value>) super.clone();
    }

    public MyMap(Map<Key, Value> data) {
        super(data);
    }

    public MyMap(int initialCapacity) {
        super(initialCapacity);
    }

    public MyMap(int initialCapacity, Key key, Value value) {
        super(initialCapacity);
        this.add(key, value);
    }

    public MyMap(Key key, Value value) {

        this.add(key, value);
    }


    public MyMap<Key, Value> addAll(Map<Key, Value> data) {
        if (data != null) {
            super.putAll(data);
        }
        return this;
    }

    /** @noinspection UnusedReturnValue*/
    public MyMap<Key, Value> add(Key key, Value value) {
        super.put(key, value);
        return this;
    }


    public <T> T get(Key key, Class<T> tClass) {
        if (!containsKey(key)) {
            return null;
        }
        var data = super.get(key);
        return JsonUtil.model2Model(data, tClass).orElse(null);
    }

    public <T1, T2> T1 get(Key key, Class<T1> tClass1, Class<T2> tClass2) {
        var data = super.get(key);
        if (!containsKey(key)) {
            return null;
        }
        return JsonUtil.model2Model(data, tClass1, tClass2).orElse(null);
    }

    public Value get(Key key, Value defaultValue) {
       /* Value value =this.get(key);
        if (value==null){
            value=defaultValue;
            this.put(key,value);
        }*/
        /*这行代码是ide提示我更改的*/
        return this.computeIfAbsent(key, k -> defaultValue);
    }

}
